import { FreeOrderHttp } from '../../../api/freeOrder.js'

const freeOrderModel = new FreeOrderHttp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    myFreeOrderMoneyInfo: {}, // 我的免单金额信息：累计订单金额、累计免单金额、累计获取免单红包
    freeOrderList: [], // 免单列表
    page: 1,
    pageSize: 10,
    totalNum: 0,
    initUser: true,
    openLogin: false,
  },

  initPage () {
    this.httpGetMyFreeOrderMoneyInfo()
    this.httpGetFreeOrderList()
  },

  onNavToDetails (event) {
    const { type } = event.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/freeOrder/details/details?type=${type}`
    })
  },

  onFreeOrder (event) {
    const { orderid } = event.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/freeOrder/invite/invite?orderId=${orderid}`
    })
  },
  
  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh () {
    this.setData({
      page: 1
    })
    this.httpGetMyFreeOrderMoneyInfo()
    this.httpGetFreeOrderList()
  },

  // 免单列表上拉加载更多
  onScrollToLower () {
    const { freeOrderList, totalNum } = this.data
    if (freeOrderList.length < totalNum) {
      this.setData({
        page: this.data.page + 1
      })
      this.httpGetFreeOrderList()
    }
  },

  // 获取我的免单金额信息
  httpGetMyFreeOrderMoneyInfo () {
    return freeOrderModel.getMyFreeOrderMoneyInfo().then((res) => {
      this.setData({
        myFreeOrderMoneyInfo: res
      })
    })
  },

  // 获取免单列表
  httpGetFreeOrderList () {
    const { freeOrderList, page, pageSize } = this.data
    return freeOrderModel.getFreeOrderList({
      page: page,
      page_size: pageSize
    }).then((res) => {
      let orderList = page === 1 ? res.free_order_list : freeOrderList.concat(res.free_order_list)
      this.setData({
        freeOrderList: orderList,
        totalNum: res.total
      })
      console.log('httpGetFreeOrderList res', res)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})